use chrono::{prelude, Utc};
use crate::coder;
// use utils::coder;

#[derive(Hash,Debug)]
pub struct BlockHeader {
    pub time: i64,  // 时间戳
    pub tx_hash: String,
    pub pre_hash: String, // 上一个block的hash
}
#[derive(Debug)]
pub struct Block {
    pub header: BlockHeader,
    pub hash: String,
    pub data: String,
}

impl Block {
    fn set_hash(&mut self) {
        let hash = coder::get_hash(&self.header);
        self.hash = hash;
    }

    pub fn new_block(data: String, pre_hash: String) -> Block {
        let tx_hash = coder::get_hash(&data);

        let time = Utc::now().timestamp();
        let header = BlockHeader {
            time,
            tx_hash,
            pre_hash,
        };

        let mut block = Block {
            header,
            hash: "".to_string(),
            data,
        };

        block.set_hash();
        block
    }
}